package com.example.swaggerDemo.service;

import com.example.swaggerDemo.Dao.userbyordeViewDao;
import com.example.swaggerDemo.entity.userbyordeView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.List;

@Resource
@Service
public class UserbyordeViewService implements userbyordeViewDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 分页查询经纪人用户订单
     * @param upuserid
     * @param typedata
     * @param statustype
     * @param pagindex
     * @param pagesize
     * @return
     * @throws SQLException
     */
    @Override
    public List<userbyordeView> findUpUserIdByType(@RequestParam("upuserid") String upuserid,
                                                          @RequestParam("typedata") String typedata,
                                                          @RequestParam("statustype") String statustype,
                                                          @RequestParam("pageindex") int pagindex,
                                                          @RequestParam("pagesize") int pagesize,
                                                   @RequestParam("username") String username

    )
            throws SQLException {
        String sql = "select * from userbyorde WHERE  upuserid= '" + upuserid + "'" +
                " and typedata=" + "'" + typedata + "'" + " and statustype=" + "'" + statustype + "'"
                + " order by id DESC " + "LIMIT "+ pagindex +","+pagesize;
        if (username!=""&&username!=null)
        {
            sql = "select * from userbyorde WHERE  upuserid= '" + upuserid + "'" +
                    " and typedata=" + "'" + typedata + "'" + " and statustype=" + "'" + statustype + "'"+" and username like "+ "'%" + username + "%'"
                    + " order by id DESC " + "LIMIT "+ pagindex +","+pagesize;
        }
        List<userbyordeView> dto = jdbcTemplate.query(sql, new BeanPropertyRowMapper(userbyordeView.class));
        return dto;
    }



    @Override
    public List<userbyordeView> findUserIdByType(@RequestParam("upuserid") String upuserid,
                                                   @RequestParam("typedata") String typedata,
                                                   @RequestParam("pageindex") int pagindex,
                                                   @RequestParam("pagesize") int pagesize
    )
            throws SQLException {
        String sql = "select * from userbyorde WHERE  upuserid= '" + upuserid + "'" +
                " and typedata=" + "'" + typedata + "'" + " order by id DESC " + "LIMIT "+ pagindex +","+pagesize;
        List<userbyordeView> dto = jdbcTemplate.query(sql, new BeanPropertyRowMapper(userbyordeView.class));
        return dto;
    }

    @Override
    public List<userbyordeView> findAssitantidByType(@RequestParam("assitantid") String assitantid,
                                                     @RequestParam("statustype") String statustype,
                                                     @RequestParam("pageindex") int pagindex,
                                                     @RequestParam("pagesize") int pagesize,
                                                     @RequestParam("username") String username
    )
            throws SQLException {
        String sql = "select * from userbyorde WHERE  assitantid= '" + assitantid + "'" +
                " and statustype=" + "'" + statustype + "'"
                + " order by id DESC " + "LIMIT "+ pagindex +","+pagesize;
        if (username!=""&&username!=null)
        {
            sql = "select * from userbyorde WHERE  assitantid= '" + assitantid + "'" +
                    " and statustype=" + "'" + statustype + "'" + " and username like " +"'%"+username+"%'"
                    + " order by id DESC " + "LIMIT "+ pagindex +","+pagesize;
        }
        List<userbyordeView> dto = jdbcTemplate.query(sql, new BeanPropertyRowMapper(userbyordeView.class));
        return dto;
    }
}
